//
// Created by Shinelon on 2024/7/6.
//

#include "tool.h"
#include <qdir.h>

#ifndef APP_BRAND
#define APP_BRAND "NetFileTransfer"
#endif


QString NetFileTransfer::readTextFile(const QString& file)
{
    QFile f(file);
    if (f.open(QFile::ReadOnly))
    {
        return f.readAll();
    }
    return "";
}

QString NetFileTransfer::getBrand()
{
    return APP_BRAND;
}

QString NetFileTransfer::splitPath(const QString& path, int* pos) {
    if (path.isEmpty()) {
        return "";
    }

    QString p = QDir::fromNativeSeparators(path);
    while (p.endsWith('/') && p.size() > 1) {
        p = p.left(p.size() - 1);
    }
    if (p.size() == 1) {
        return p;
    }

    *pos = p.lastIndexOf("/");
    return p;
}

QString NetFileTransfer::getBaseName(const QString& path) {
    int pos;
    QString p = splitPath(path, &pos);
    if (p.size() <= 1) {
        return p;
    }

    if (pos == -1) {
        return p;
    }
    return p.mid(pos + 1);
}